/*
 * Created by SharpDevelop.
 * user: Robertux
 * Date: 20/03/2008
 * Time: 11:20
 * 
 * 
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using DataLayer;
using Inventador;

namespace WinApp
{
	/// <summary>
	/// Description of frmEntradasSalidas.
	/// </summary>
	public partial class frmPrecios : Form
	{
		private BaseDatos bdatos;
		private List<Producto> prods;
		private List<Precio> precios;
		private bool prodIsValid = true;
		
		public frmPrecios()
		{
			InitializeComponent();
		}
		
		public frmPrecios(BaseDatos bdatosExterna, Producto pr): this()
		{
			this.bdatos = bdatosExterna;
			this.prods = Producto.GetList(this.bdatos, "id != 0");
			this.RellenarComboProds();
			this.comboBox1.SelectedIndex = 0;
			this.precios = Precio.GetList(this.bdatos, "id != 0 AND producto=" + this.prods[this.comboBox1.SelectedIndex].Id.ToString());
			this.RellenarGrid();
			this.gridManager1.AutoGenerarColumnas();
			if(pr != null)
				this.SelectProducto(pr);
		}
		
		public void RellenarComboProds()
		{
			this.comboBox1.AutoCompleteSource = AutoCompleteSource.None;
			this.comboBox1.Items.Clear();
			foreach(Producto prd in this.prods)
				this.comboBox1.Items.Add(prd.fkTipoProducto.Nombre + " " + prd.fkMarcaProducto.Nombre);
			this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;			
		}
		
		public void RellenarGrid()
		{
			this.grdPrecios.Rows.Clear();
			foreach(Precio pr in this.precios)
			{
				this.grdPrecios.Rows.Add(new object[]{pr.Id, pr.Fecha.ToShortDateString(), pr.Valor, "Eliminar"});
			}			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			this.Close();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			if(this.prodIsValid)
			{
				frmAddPrecio frmaddprc = new frmAddPrecio(this.bdatos, this.prods[this.comboBox1.SelectedIndex]);
				frmaddprc.ShowDialog(this);
				if(frmaddprc.DialogResult == DialogResult.OK)
				{
					this.precios = Precio.GetList(this.bdatos, "id != 0 AND producto=" + this.prods[this.comboBox1.SelectedIndex].Id.ToString());
					this.RellenarGrid();
				}
			}
		}
		
		void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
		{
			if(this.prodIsValid)
			{
				this.precios = Precio.GetList(this.bdatos, "id != 0 AND producto = " + this.prods[this.comboBox1.SelectedIndex].Id.ToString());
				this.RellenarGrid();			
				this.Text = "Precios del Producto " + this.prods[this.comboBox1.SelectedIndex].fkTipoProducto.Nombre + " " + this.prods[this.comboBox1.SelectedIndex].fkMarcaProducto.Nombre;
			}
		}
		
		void ComboBox1Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			bool bandera = false;
			foreach(Producto prod in this.prods)
			{
				if((prod.fkTipoProducto.Nombre + " " + prod.fkMarcaProducto.Nombre) == this.comboBox1.Text)
				{
					bandera = true;
					break;
				}
			}
			if(!bandera)
			{
				errp.SetError(this.comboBox1, "El texto escrito no coincide con ningun producto");
				this.prodIsValid = false;
			}
			else
			{
				errp.SetError(this.comboBox1, "");
				this.prodIsValid = true;
			}
		}
		
		void GrdPreciosCellClick(object sender, DataGridViewCellEventArgs e)
		{
			if(this.prodIsValid)
			{
				if(e.ColumnIndex == 3)
				{
					this.precios[e.RowIndex].Delete(this.bdatos);
					this.grdPrecios.Rows.RemoveAt(e.RowIndex);
				}
			}
		}
		
		public void SelectProducto(Producto pr)
		{
			for(int i = 0; i < this.comboBox1.Items.Count; i++)
			{
				if(this.comboBox1.Items[i].ToString() == (pr.fkTipoProducto.Nombre + " " + pr.fkMarcaProducto.Nombre))
				{
					this.comboBox1.SelectedIndex = i;
					break;
				}
			}
		}
	}
}
